<template>
  <div>
    <!--  根据条件查询-->
    <div>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="选择一级种类名称:">
          <el-select v-model="pid,value" placeholder="选择一级种类" @change="changeLevel2(value)">
            <el-option value='' label="请选择"></el-option>
            <el-option @click.native="getPid(item.pid)"
                       v-for="item in fileKind"
                       :key="item.id"
                       :label="item.kindName"
                       :value="item.id">
            </el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="选择二级种类名称:" v-if="fileKind2!=null&&fileKind2!=''">
          <el-select v-model="value2" placeholder="选择二级种类" @change="changeLevel3(value2)">
            <el-option value='' label="请选择"></el-option>
            <el-option
                v-for="item in fileKind2"
                :key="item.id"
                :label="item.kindName"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择三级种类名称:" v-if="fileKind3!=null&&fileKind3!=''">
          <el-select v-model="value3" placeholder="选择三级种类">
            <el-option value='' label="请选择"></el-option>
            <el-option
                v-for="item in fileKind3"
                :key="item.id"
                :label="item.kindName"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品名称:">
          <el-input v-model="name" placeholder="产品名称"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="init">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--编号,名称,档次级别,1级分类,二级分类,三级分类,用途类型,产品经理  -->
    <div>
      <el-table
          :data="fileData"
          border
          style="width: 100%">
        <el-table-column
            fixed
            prop="productId"
            label="产品编号"
            width="150">
        </el-table-column>
        <el-table-column
            prop="productName"
            label="产品名称"
            width="120">
        </el-table-column>
        <el-table-column
            prop="productClass"
            label="档次级别"
            width="120">
        </el-table-column>
        <el-table-column
            prop="firstKindName"
            label="一级分类名称"
            width="120">
        </el-table-column>
        <el-table-column
            prop="secondKindName"
            label="二级分类名称"
            width="120">
        </el-table-column>
        <el-table-column
            prop="thirdKindName"
            label="三级分类名称"
            width="120">
        </el-table-column>
        <el-table-column
            prop="type"
            label="用途类型"
            width="120">
          <template slot-scope="scope">
            <p v-if="fileData[scope.$index].type=='Y001-1'">商品</p>
            <p v-else-if="fileData[scope.$index].type=='Y001-2'">物料</p>
            <p v-else>无法识别</p>
          </template>
        </el-table-column>
        <el-table-column
            prop="responsiblePerson"
            label="产品经理"
            width="120">
        </el-table-column>
        <el-table-column
            fixed="right"
            label="操作"
            width="100">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="medium">产品修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--分页    -->
    <div class="block">
      <el-pagination
          @size-change="handleSizeChange"
          :current-page="Number(pageNum)"
          :page-sizes="[2, 4, 8, 16]"
          :page-size="Number(pageSize)"
          @current-change="handleCurrentChange"
          layout="total, sizes, prev, pager, next, jumper"
          :total="Number(total)">
      </el-pagination>
    </div>

    <!-- 弹出框 -->
    <el-dialog title="产品信息详情:" :visible.sync="dialogVisible" width="60%" :before-close="handleClose">
      <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>您正在做的业务是:</el-breadcrumb-item>
          <el-breadcrumb-item>生产设计</el-breadcrumb-item>
          <el-breadcrumb-item>产品档案管理</el-breadcrumb-item>
          <el-breadcrumb-item>产品档案登记</el-breadcrumb-item>
        </el-breadcrumb>
        <el-divider content-position="left">主信息</el-divider>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm">
          <el-form-item label="产品名称" prop="productName">
            <el-input v-model="ruleForm.productName"></el-input>
          </el-form-item>
          <el-form-item label="制造商" prop="factoryName">
            <el-input v-model="ruleForm.factoryName"></el-input>
          </el-form-item>
          <el-form-item label="产品简称" prop="productNick">
            <el-input v-model="ruleForm.productNick"></el-input>
          </el-form-item>
          <el-form-item label="选择一级种类名称:">
            <el-select v-model="pid22,value11" placeholder="选择一级种类" @change="changeLevel22(value11)" ref="selectCh">
              <el-option value='' label="请选择"></el-option>
              <el-option @click.native="getPid22(item.pid)"
                         v-for="item in fileKind11"
                         :key="item.id"
                         :label="item.kindName"
                         :value="item.id">
              </el-option>
            </el-select>

          </el-form-item>
          <el-form-item label="选择二级种类名称:">
            <el-select v-model="value22" placeholder="选择二级种类" @change="changeLevel33(value22)" ref="selectCh">
              <el-option value='' label="请选择"></el-option>
              <el-option
                  v-for="item in fileKind22"
                  :key="item.id"
                  :label="item.kindName"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="选择三级种类名称:">
            <el-select v-model="value33" placeholder="选择三级种类" @change="changeLevel4(value33)" ref="selectCh">
              <el-option value='' label="请选择"></el-option>
              <el-option
                  v-for="item in fileKind33"
                  :key="item.id"
                  :label="item.kindName"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="用途类别" prop="type">
            <el-select v-model="ruleForm.type" placeholder="请选择用途类别">
              <el-option label="商品" value="Y001-1"></el-option>
              <el-option label="物料" value="Y001-2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="档次级别" prop="productClass">
            <el-select v-model="ruleForm.productClass" placeholder="请选择档次级别">
              <el-option label="高档" value="D001-1"></el-option>
              <el-option label="中档" value="D001-2"></el-option>
              <el-option label="低档" value="D001-3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="计量单位" prop="personalUnit">
            <el-input v-model="ruleForm.personalUnit"></el-input>
          </el-form-item>
          <el-form-item label="计量值" prop="personalValue">
            <el-input v-model="ruleForm.personalValue"></el-input>
          </el-form-item>
          <el-form-item label="市场单价" prop="listPrice">
            <el-input v-model="ruleForm.listPrice"></el-input>
          </el-form-item>
          <el-form-item label="计划成本单价" prop="costPrice">
            <el-input v-model="ruleForm.costPrice"></el-input>
          </el-form-item>

          <el-divider content-position="left">辅助信息</el-divider>
          <el-form-item label="保修期" prop="warranty">
            <el-input v-model="ruleForm.warranty"></el-input>
          </el-form-item>
          <el-form-item label="替代品名称" prop="twinName">
            <el-input v-model="ruleForm.twinName"></el-input>
          </el-form-item>
          <el-form-item label="替代品编号" prop="twinId">
            <el-input v-model="ruleForm.twinId"></el-input>
          </el-form-item>
          <el-form-item label="生命周期(年)" prop="lifecycle">
            <el-input v-model="ruleForm.lifecycle"></el-input>
          </el-form-item>
          <el-form-item label="单位" prop="amountUnit">
            <el-input v-model="ruleForm.amountUnit"></el-input>
          </el-form-item>
          <el-form-item label="产品描述" prop="productDescribe">
            <el-input type="textarea" v-model="ruleForm.productDescribe"></el-input>
          </el-form-item>
          <el-form-item label="产品经理" prop="responsiblePerson">
            <el-input v-model="ruleForm.responsiblePerson"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
            <el-button @click="resetForm()">退出</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import request from "../../request.js"

export default {
  name: "fileUpdate",
  data() {
    return {
      ruleForm: {
        productName: '',
        factoryName: '',
        productNick: '',
        type: '',
        productClass: '',
        firstKindId: '',
        firstKindName: '',
        secondKindId: '',
        secondKindName: '',
        thirdKindId: '',
        thirdKindName: '',
        warranty: '',
        twinName: '',
        twinId: '',
        lifecycle: '',
        amountUnit: '',
        productDescribe: '',
        responsiblePerson: '',
        personalValue: '',
        personalUnit: '',
        listPrice: '',
        costPrice: '',
      },
      rules: {
        productName: [
          {required: true, message: '请输入产品名称', trigger: 'blur'},
        ],
        type: [
          {required: true, message: '请选择用途类别', trigger: 'change'}
        ],
        productClass: [
          {required: true, message: '请选择档次级别', trigger: 'change'}
        ],
        firstKindId: [
          {required: true, message: '请选择一级分类', trigger: 'change'}
        ],
        secondKindId: [
          {required: true, message: '请选择二级分类', trigger: 'change'}
        ],
        thirdKindId: [
          {required: true, message: '请选择三级分类', trigger: 'change'}
        ],

        listPrice: [
          {required: true, message: '请输入市场单价', trigger: 'blur'}
        ],
        costPrice: [
          {required: true, message: '请输入计划成本单价', trigger: 'blur'}
        ],
      },
      dialogVisible: false,
      formInline: {
        user: '',
        region: ''
      },
      pid: '',
      value: '',
      value11: '',
      value2: '',
      value22: '',
      value3: '',
      value33: '',
      pid22: '',
      fileKind: [],
      fileKind11: [],
      fileKind2: [],
      fileKind22: [],
      fileKind3: [],
      fileKind33: [],
      //查询数据
      pageNum: '',
      pageSize: '',
      name: '',
      total: '',
      //初始化展示数据
      fileData: [],
      fileDataDetail: {}
    }
  }, methods: {
    handleClick(row) {

      axios.get("http://localhost:8150/file/queryByID", {
        params: {
          'id': row.id
        }
      }).then(data => {
        this.ruleForm = data.data.data;
        this.value11 = this.ruleForm.firstKindId;
        this.value22 = this.ruleForm.secondKindId;
        this.value33 = this.ruleForm.thirdKindId;
        // this.getPid22(this.pid22);
        this.changeLevel22(this.ruleForm.firstKindId);
        this.changeLevel33(this.ruleForm.secondKindId);
        this.changeLevel4(this.ruleForm.thirdKindId);
      })
      this.dialogVisible = true;
    },
    //初始化数据
    init() {
      axios.get("http://localhost:8150/file/queryProduct", {
        params: {
          'name': this.name,
          'firstId': this.value,
          'secondId': this.value2,
          'threeId': this.value3,
          'pageNum': this.pageNum,
          'pageSize': this.pageSize
        }
      }).then(data => {
        if (data.data.flag==false){
          this.$message({
            showClose:true,
            message:data.data.message,
            type:data.data.flag==true?"success":"error"
          })
          return;
        }
        this.fileData = data.data.data.records;
        this.pageNum = data.data.data.current;
        this.pageSize = data.data.data.size;
        this.total = data.data.data.total;
      }).catch(e => {
        alert("错误信息为:" + e)
      })
    },
    handleSizeChange(val) {
      this.pageSize = val;
      console.log(`每页 ${val} 条`);
      this.init();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      console.log(`当前页: ${val}`);
      this.init();
    },
    changeLevel2(id) {
      this.ruleForm.firstKindId = id;
      for (let i = 0; i < this.fileKind.length; i++) {
        if (id == this.fileKind[i].id) {
          this.ruleForm.firstKindName = this.fileKind[i].kindName;
        } else {
          this.ruleForm.firstKindName = '';
          this.ruleForm.firstKindId = '';
          this.value = '';
        }
      }
      console.log("value:" + id)
      let a = 0;
      for (let i = 0; i < this.fileKind.length; i++) {
        for (let j = 0; j < this.fileKind[i].list.length; j++) {
          if (id === this.fileKind[i].list[j].pid) {
            this.fileKind2[a] = this.fileKind[i].list[j];
            a++;
          } else {
            this.fileKind2 = [];
            this.fileKind3 = [];
            this.value2 = '';
            this.value3 = '';
          }
        }
      }
    }, changeLevel3(id) {
      for (let i = 0; i < this.fileKind2.length; i++) {
        if (id == this.fileKind2[i].id) {
          this.ruleForm.secondKindName = this.fileKind2[i].kindName;
        } else {
          this.ruleForm.secondKindName = '';
          this.ruleForm.secondKindId = '';
          this.value3 = '';
        }
      }
      //  console.log(id)
      let a = 0;

      for (let i = 0; i < this.fileKind2.length; i++) {
        for (let j = 0; j < this.fileKind2[i].list.length; j++) {
          if (id == this.fileKind2[i].list[j].pid) {
            console.log(this.fileKind2[i].list[j].pid)
            this.fileKind3[a] = this.fileKind2[i].list[j];
            a++;
          } else {
            this.fileKind3 = [];
            this.value3 = '';
          }
        }
      }
    }, getPid(id) {
      this.pid = id;
    },//获取下拉框的值

    changeLevel22(id) {
      this.ruleForm.firstKindId = id;
      for (let i = 0; i < this.fileKind11.length; i++) {
        if (id == this.fileKind11[i].id) {
          this.ruleForm.firstKindName = this.fileKind11[i].kindName;
        } else {
          this.ruleForm.firstKindName = '';
          this.ruleForm.firstKindId = '';
          this.value11 = '';
        }
      }
      console.log("value:" + id)
      let a = 0;
      for (let i = 0; i < this.fileKind11.length; i++) {
        for (let j = 0; j < this.fileKind11[i].list.length; j++) {
          if (id === this.fileKind11[i].list[j].pid) {
            this.fileKind22[a] = this.fileKind11[i].list[j];
            a++;
          } else {
            this.fileKind22 = [];
            this.fileKind33 = [];
            this.value22 = '';
            this.value33 = '';
          }
        }
      }
    }, changeLevel33(id) {
      for (let i = 0; i < this.fileKind22.length; i++) {
        if (id == this.fileKind22[i].id) {
          this.ruleForm.secondKindName = this.fileKind22[i].kindName;
        } else {
          this.ruleForm.secondKindName = '';
          this.ruleForm.secondKindId = '';
          this.value33 = '';
        }
      }
      //  console.log(id)
      let a = 0;

      for (let i = 0; i < this.fileKind22.length; i++) {
        for (let j = 0; j < this.fileKind22[i].list.length; j++) {
          if (id == this.fileKind22[i].list[j].pid) {
            this.fileKind33[a] = this.fileKind22[i].list[j];
            a++;
          } else {
            this.fileKind33 = [];
            this.value33 = '';
          }
        }
      }
    },
    changeLevel4(id) {
      this.ruleForm.thirdKindId = id;
      for (let i = 0; i < this.fileKind33.length; i++) {
        if (id == this.fileKind33[i].id) {
          this.ruleForm.thirdKindName = this.fileKind33[i].kindName;
        } else {
          this.ruleForm.thirdKindId = '';
        }
      }
    }, getPid22(id) {
      this.pid22 = id;
    },//获取下拉框的值
    getSelectValue() {
      axios.get("http://localhost:8150/config-file-kind/displayType").then(data => {
  /*      if (data.data.flag==false){
          this.$message({
            showClose:true,
            message:data.data.message,
            type:data.data.flag==true?"success":"error"
          })
          return;
        }*/
        this.fileKind = data.data.data;
        this.fileKind11 = data.data.data;
      }).catch(e => {
        alert("错误信息为:" + e)
      });
    }, handleClose() {
      this.value11 = '';
      this.value22 = '';
      this.value33 = '';
      this.pid22 = '';
      this.ruleForm = {};
      // this.fileKind11=[];
      this.fileKind22 = [];
      this.fileKind33 = [];
      this.dialogVisible = false;
    },
    resetForm() {
      this.value11 = '';
      this.value22 = '';
      this.value33 = '';
      this.pid22 = '';
      this.ruleForm = {};
      //  this.fileKind11=[];
      this.fileKind22 = [];
      this.fileKind33 = [];
      this.dialogVisible = false;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post("http://localhost:8150/file/updateFile", this.ruleForm).then(resp => {
            if (resp.data.flag==false){
              this.$message({
                showClose:true,
                message:resp.data.message,
                type:resp.data.flag==true?"success":"error"
              })
              return;
            }

            if (resp.data.flag) {
              this.ruleForm = {};
              this.value11 = '';
              this.value22 = '';
              this.value33 = '';
              this.dialogVisible = false;
              this.init();
              this.getSelectValue();
            }
            this.$message({
              showClose: true,
              message: resp.data.message,
              type: resp.data.flag == true ? 'success' : 'error'
            });
          }).catch(e => {
            alert("错误提示:" + e);
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },
  }, created() {
    this.getSelectValue();
    this.init();
  }
}
</script>

<style scoped>

</style>